/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F288
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include	"SN32F280.h"
#include	"SN32F200_Def.h"
#include "LED_Driver.h"
#include "..\Driver\CT16.h"
#include "..\Driver\PWM.h"
#include "..\KB\KB_Scan.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/


/*_____ D E F I N I T I O N S ______________________________________________*/
uint16_t iRPWM_Buf[LED_ALL_NUM];
uint16_t iGPWM_Buf[LED_ALL_NUM];
uint16_t iBPWM_Buf[LED_ALL_NUM];
uint8_t RPWM_Base_Buf[LED_NUM];
uint8_t GPWM_Base_Buf[LED_NUM];
uint8_t BPWM_Base_Buf[LED_NUM];
uint8_t rPWM_DutyBuf[LED_NUM];
uint8_t rROW_NO = 0;
uint8_t rKeyScanTimer = 0;
uint8_t rKeyCol_NO = 0;
/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/
/*****************************************************************************
* Function		: LED_Driver
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void LED_MRn_Load(void)
{
	uint16_t *P_Temp;
	switch(rROW_NO)
	{
		case 0:			P_Temp =&iRPWM_Buf[0];			break;
		case 1:			P_Temp =&iRPWM_Buf[21];			break;
		case 2:			P_Temp =&iRPWM_Buf[42];			break;
		case 3:			P_Temp =&iRPWM_Buf[63];			break;
		case 4:			P_Temp =&iRPWM_Buf[84];			break;
		case 5:			P_Temp =&iRPWM_Buf[105];		break;
		case 6:			P_Temp =&iRPWM_Buf[126];		break;		
		case 7:			P_Temp =&iGPWM_Buf[0];			break;
		case 8:			P_Temp =&iGPWM_Buf[21];			break;
		case 9:			P_Temp =&iGPWM_Buf[42];			break;
		case 10:		P_Temp =&iGPWM_Buf[63];			break;
		case 11:		P_Temp =&iGPWM_Buf[84];			break;
		case 12:		P_Temp =&iGPWM_Buf[105];		break;
		case 13:		P_Temp =&iGPWM_Buf[126];		break;
		case 14:		P_Temp =&iBPWM_Buf[0];			break;
		case 15:		P_Temp =&iBPWM_Buf[21];			break;
		case 16:		P_Temp =&iBPWM_Buf[42];			break;
		case 17:		P_Temp =&iBPWM_Buf[63];			break;
		case 18:		P_Temp =&iBPWM_Buf[84];			break;
		case 19:		P_Temp =&iBPWM_Buf[105];		break;
		case 20:		P_Temp =&iBPWM_Buf[126];		break;
		default:
			break;
	}		
	SN_CT16B1->MR4 = PWMKEY|(*(P_Temp+0));
	SN_CT16B1->MR5 = PWMKEY|(*(P_Temp+1));
	SN_CT16B1->MR8 = PWMKEY|(*(P_Temp+2));
	SN_CT16B2->MR1 = PWMKEY|(*(P_Temp+3));
	SN_CT16B2->MR2 = PWMKEY|(*(P_Temp+4));
	SN_CT16B2->MR3 = PWMKEY|(*(P_Temp+5));
	SN_CT16B1->MR6 = PWMKEY|(*(P_Temp+6));
	SN_CT16B1->MR7 = PWMKEY|(*(P_Temp+7));
	SN_CT16B1->MR9 = PWMKEY|(*(P_Temp+8));
	SN_CT16B2->MR0 = PWMKEY|(*(P_Temp+9));
	SN_CT16B0->MR0 = PWMKEY|(*(P_Temp+10));
	SN_CT16B1->MR10 = PWMKEY|(*(P_Temp+11));
	SN_CT16B0->MR3 = PWMKEY|(*(P_Temp+12));
	SN_CT16B0->MR2 = PWMKEY|(*(P_Temp+13));
	SN_CT16B0->MR1 = PWMKEY|(*(P_Temp+14));
	SN_CT16B1->MR0 = PWMKEY|(*(P_Temp+15));
	SN_CT16B1->MR1 = PWMKEY|(*(P_Temp+16));	
	SN_CT16B1->MR2 = PWMKEY|(*(P_Temp+17));	
	SN_CT16B1->MR3 = PWMKEY|(*(P_Temp+18));	
}

/*****************************************************************************
* Function		: LED_IO_Driver
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void LED_IO_Driver(void)
{
	uint8_t i;
	switch(rROW_NO)
	{
		case 0:	
		{
			for(i=0;i<21;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW1_HIGH;		
					break;							
				}
			}	
			break;
		}			
		case 1:	
		{
			for(i=21;i<42;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW2_HIGH;		
					break;							
				}
			}	
			break;
		}		
		case 2:	
		{
			for(i=42;i<63;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW3_HIGH;		
					break;							
				}
			}	
			break;
		}	
		case 3:	
		{
			for(i=63;i<84;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW4_HIGH;		
					break;							
				}
			}	
			break;
		}			
		case 4:	
		{
			for(i=84;i<105;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW5_HIGH;			
					break;							
				}
			}	
			break;		
		}			
		case 5:	
		{
			for(i=105;i<126;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW6_HIGH;		
					break;							
				}
			}	
			break;		
		}	
		case 6:	
		{
			for(i=126;i<147;i++)
			{
				if(iRPWM_Buf[i] != 0)
				{
					R_ROW7_HIGH;		
					break;							
				}
			}	
			break;		
		}	
		
		case 7:	
		{
			for(i=0;i<21;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW1_HIGH;		
					break;							
				}
			}	
			break;
		}			
		case 8:	
		{
			for(i=21;i<42;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW2_HIGH;		
					break;							
				}
			}	
			break;
		}		
		case 9:	
		{
			for(i=42;i<63;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW3_HIGH;
					break;					
				}
			}	
			break;
		}	
		case 10:	
		{
			for(i=63;i<84;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW4_HIGH;			
					break;							
				}
			}	
			break;
		}			
		case 11:	
		{
			for(i=84;i<105;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW5_HIGH;			
					break;						
				}
			}	
			break;		
		}			
		case 12:	
		{
			for(i=105;i<126;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW6_HIGH;	
					break;						
				}
			}	
			break;		
		}	
		case 13:	
		{
			for(i=126;i<147;i++)
			{
				if(iGPWM_Buf[i] != 0)
				{
					G_ROW7_HIGH;	
					break;						
				}
			}	
			break;		
		}	
		
		case 14:	
		{
			for(i=0;i<21;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW1_HIGH;	
					break;						
				}
			}	
			break;
		}			
		case 15:	
		{
			for(i=21;i<42;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW2_HIGH;		
					break;									
				}
			}	
			break;
		}		
		case 16:	
		{
			for(i=42;i<63;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW3_HIGH;	
					break;							
				}
			}	
			break;
		}	
		case 17:	
		{
			for(i=63;i<84;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW4_HIGH;	
					break;							
				}
			}	
			break;
		}			
		case 18:	
		{
			for(i=84;i<105;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW5_HIGH;		
					break;							
				}
			}	
			break;		
		}			
		case 19:	
		{
			for(i=105;i<126;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW6_HIGH;	
					break;						
				}
			}	
			break;		
		}	
		case 20:	
		{
			for(i=126;i<147;i++)
			{
				if(iBPWM_Buf[i] != 0)
				{
					B_ROW7_HIGH;	
					break;						
				}
			}	
			break;		
		}	
		default:
			break;
	}		
}
/*****************************************************************************
* Function		: All_LedRow_OutputLow
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void All_LedRow_OutputLow(void)
{	
	SN_GPIO0->BCLR |= ROW_P0_MASK;	
	SN_GPIO1->BCLR |= ROW_P1_MASK;	
	SN_GPIO2->BCLR |= ROW_P2_MASK;	
	SN_GPIO3->BCLR |= ROW_P3_MASK;
}

/*****************************************************************************
* Function		 : CT16B3_IRQHandler
* Description	 : 255us Timer ISR
* Input	(Global) : None
* Input	(Local)	 : None
* Output (Global): None
* Return (Local) : None
*****************************************************************************/
__irq void CT16B3_IRQHandler(void)
{
  if(SN_CT16B3->RIS_b.MR0IF)
  {
    SN_CT16B3->TC = 0;
    SN_CT16B3->TMRCTRL &= mskCT16_CEN_DIS;
		All_LedRow_OutputLow();						//led row output High for turnoff LED

    SN_CT16B1->TMRCTRL &= mskCT16_CEN_DIS;
    SN_CT16B0->TMRCTRL &= mskCT16_CEN_DIS;
    SN_CT16B2->TMRCTRL &= mskCT16_CEN_DIS;
    SN_CT16B5->TMRCTRL &= mskCT16_CEN_DIS;
		Reset_All_PWM();

    SN_CT16B0->TC = 0;
    SN_CT16B1->TC = 0;
    SN_CT16B2->TC = 0;
    SN_CT16B5->TC = 0;  
		
		Disable_All_PWM();
		All_Col_SetInput();
		if(bmKeyScanTimer == 0)
		{
			bufKB_InMap[rKeyCol_NO] = KB_GetRowStatus();
			rKeyCol_NO++;
			if(rKeyCol_NO >= KB_TABLE_COLUMN_SIZE)
			{
				rKeyCol_NO = 0;		
				bmKeyScanTimer = 1;
			}

      bufKB_InMap[rKeyCol_NO] = KB_GetRowStatus();
			rKeyCol_NO++;
			if(rKeyCol_NO>=KB_TABLE_COLUMN_SIZE)
			{
				rKeyCol_NO = 0;		
				bmKeyScanTimer = 1;
			}

			bufKB_InMap[rKeyCol_NO] = KB_GetRowStatus();
			rKeyCol_NO++;
			if(rKeyCol_NO >= KB_TABLE_COLUMN_SIZE)
			{
				rKeyCol_NO = 0;		
				bmKeyScanTimer = 1;
			}			
		}
		
		Enable_All_PWM();
		LED_MRn_Load();
		LED_IO_Driver();
    
		SN_CT16B0->TMRCTRL |= mskCT16_CEN_EN;  
		SN_CT16B1->TMRCTRL |= mskCT16_CEN_EN;
		SN_CT16B2->TMRCTRL |= mskCT16_CEN_EN;
		SN_CT16B5->TMRCTRL |= mskCT16_CEN_EN;
		SN_CT16B3->TMRCTRL |= mskCT16_CEN_EN;
  }
	SN_CT16B3->IC = mskCT16_MR0IC;	//Clear MR0 match interrupt status
	rROW_NO++;
	if(rROW_NO >= 21)
		rROW_NO = 0;
}
/*****************************************************************************
* Function		 : Flash_KeyScan
* Description	 : 255us Timer ISR
* Input	(Global) : None
* Input	(Local)	 : None
* Output (Global): None
* Return (Local) : None
*****************************************************************************/
void Flash_KeyScan(void)
{
	for(rKeyCol_NO = 0; rKeyCol_NO<KB_TABLE_COLUMN_SIZE;rKeyCol_NO++)
		bufKB_InMap[rKeyCol_NO] = KB_GetRowStatus();	
	bmKeyScanTimer = 1;		
}

